package ln.fxspringarchetype.controller.student;

import javafx.beans.binding.Bindings;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import ln.fxspringarchetype.controller.BaseFxController;
import ln.fxspringarchetype.vo.StudentVO;
import org.apache.logging.log4j.util.Strings;

import java.io.ByteArrayInputStream;
import java.util.Objects;

public class DeleteStudentController extends BaseFxController {
    private final StudentVO studentVO = new StudentVO();
    @FXML
    private Label idLabel;
    @FXML
    private Label nameLabel;
    @FXML
    private Label birthdayLabel;
    @FXML
    private Label genderLabel;
    @FXML
    private ImageView photoView;

    public StudentVO getStudentVO() {
        return studentVO;
    }

    @Override
    public void setCellValueFactories() {

    }

    @Override
    public void setCellFactories() {

    }

    @Override
    public void createBindings() {
        idLabel.textProperty().bind(
                Bindings.createStringBinding(
                        () -> Integer.toString(studentVO.getId()), studentVO.idProperty()));
        nameLabel.textProperty().bind(studentVO.nameProperty());
        genderLabel.textProperty().bind(studentVO.genderProperty());
        birthdayLabel.textProperty().bind(
                Bindings.createStringBinding(
                        () -> {
                            if (Objects.nonNull(studentVO.getBirthday())) {
                                return studentVO.getBirthday().toString();
                            } else {
                                return Strings.EMPTY;
                            }
                        }, studentVO.birthdayProperty()));
        photoView.imageProperty().bind(
                Bindings.createObjectBinding(
                        () -> {
                            if (Objects.nonNull(studentVO.getPhoto())) {
                                return getImage(studentVO.getPhoto());
                            } else {
                                return getImage(new byte[0]);
                            }
                        }, studentVO.photoProperty()));
    }

    @Override
    public void initLayout() {

    }

    @Override
    public void loadData() {

    }

    private Image getImage(byte[] imageBytes) {
        return new Image(new ByteArrayInputStream(imageBytes));
    }
}
